/* -------------------------------------- 

     Cherrypy webapp skeleton frontend

     License: LGPL
     Authors: saaj (mail@saaj.me)

   -------------------------------------- */

/*
#asset(qx/icon/${qx.icontheme}/16/apps/utilities-text-editor.png) 
#asset(qx/icon/${qx.icontheme}/16/actions/list-add.png) 
*/
   
/**
 * File editor view
 */
qx.Class.define("webapp.view.FileEditor", {

	extend : webapp.view.AbstractEditor,

  members : {

    _listController : null,
    
    
  	_getFormHeading : function()
  	{
      return {
        caption : this.tr("New file"),
        icon    : "icon/16/apps/utilities-text-editor.png"
      };
  	},

    _onAddFile : function(event)
    {
      var file = event.getData();
      
      this._listController.getModel().push(qx.data.marshal.Json.createModel({name : file.getFilename()}));
      
      var stateListenerId = file.addListener("changeState", function(event) 
      {
        var state = event.getData();
        if(state == "uploaded")
        {
          this.getModel().notifyFileSaved(file.getFilename());
          file.removeListenerById(stateListenerId);
        }
      }, this);
    },
    
  	_createForm : function()
  	{
      this.base(arguments);

      var icon     = "icon/16/actions/list-add.png";
      var addFiles = new com.zenesis.qx.upload.UploadButton(this.tr("Add files"), icon);
      addFiles.setWidth(90);
      this._form.addButton(addFiles);
      
      var uploadManager = new com.zenesis.qx.upload.UploadMgr();
      uploadManager.setAutoUpload(false);
      uploadManager.setMultiple(true);
      uploadManager.addWidget(addFiles);
      uploadManager.addListener("addFile", this._onAddFile, this);
      
      this.getModel().addListener("loaded", function()
      {
        this.getModel().getFormModel().setUploader(uploadManager);
      }, this);
      
      
      
      var list             = new qx.ui.form.List();
      this._listController = new qx.data.controller.List(new qx.data.Array(), list, "files");
      this._listController.setLabelPath("name");
      
      this._form.add(list, this.tr("Files"), null, "files");
  	}
    
  }

});